﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Silverlight.Training.Server.Common;
using Silverlight.Training.Server.Common.Contracts;
using Silverlight.Training.Server.Common.Feedbacks;
using Silverlight.Training.Server.Interfaces;

namespace Silverlight.Training.Server.DAOs
{
    public class EmployeeTypeDAO : IEmployeeTypeDAO
    {
        /// <summary>
        /// This method aims to get all employee types from database
        /// </summary>
        /// <returns>
        /// A Feedback object containing all employee types
        /// </returns>
        public CollectionFeedback<EmployeeType> GetCollection()
        {
            try
            {
                SqlConnection connection = new SqlConnection(ServiceConstants.ConnectionString);
                SqlCommand command = new SqlCommand("GetEmployeeTypeCollection", connection);
                command.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable dt = new DataTable("Table");
                adapter.Fill(dt);
                List<EmployeeType> list = new List<EmployeeType>();
                if (dt.Rows.Count != 0)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        var result = new EmployeeType
                        {
                            ID = Convert.ToInt32(dataRow["ID"]),
                            TypeName = Convert.ToString(dataRow["TypeName"]),
                            Description = Convert.ToString(dataRow["Description"])
                        };

                        list.Add(result);
                    }
                }
                return new CollectionFeedback<EmployeeType>("Get employee type collection successfully!", true, list);
            }
            catch (SqlException e)
            {
                return new CollectionFeedback<EmployeeType>("Get employee type collection unsuccessfully! " + e.Message, false, null); ;
            }
            catch (Exception e)
            {
                return new CollectionFeedback<EmployeeType>("Get employee type collection unsuccessfully! " + e.Message, false, null); ;
            }
        }

        /// <summary>
        /// This method aims to get detailed information of an employee type based on its ID.
        /// </summary>
        /// <param name="eTypeID">
        /// The ID of the desired employee type
        /// </param>
        /// <returns>
        /// A Feedback object containing a detailed employee type
        /// </returns>
        public SingleFeedback<EmployeeType> GetSingle(int eTypeID)
        {
            try
            {
                SqlConnection connection = new SqlConnection(ServiceConstants.ConnectionString);
                SqlCommand command = new SqlCommand("GetSingleEmployeeType", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@eTypeID", eTypeID);

                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable dt = new DataTable("Table");
                adapter.Fill(dt);
                if (dt.Rows.Count == 1)
                {
                    foreach (DataRow dataRow in dt.Rows)
                    {
                        var result = new EmployeeType
                        {
                            ID = Convert.ToInt32(dataRow["ID"]),
                            TypeName = Convert.ToString(dataRow["TypeName"]),
                            Description = Convert.ToString(dataRow["Description"])
                        };

                        return new SingleFeedback<EmployeeType>("Get employee type details successfully!", true, result);
                    }
                }
                return new SingleFeedback<EmployeeType>("Get employee type details unsuccessfully! Invalid reult.", false, null);
            }
            catch (SqlException e)
            {
                return new SingleFeedback<EmployeeType>("Get employee type details unsuccessfully! " + e.Message, false, null);
            }
            catch (Exception e)
            {
                return new SingleFeedback<EmployeeType>("Get employee type details unsuccessfully! " + e.Message, false, null);
            }
        }

        /// <summary>
        /// This method aims to insert a new object into database
        /// </summary>
        /// <param name="data">
        /// The EmployeeType object to be inserted
        /// </param>
        /// <returns>
        /// A Feedback object containing information about 
        ///    - inserting result and 
        ///    - the ID of new object if success or -1
        /// </returns>
        public SingleFeedback<int> Insert(EmployeeType data)
        {
            return null;
        }

        /// <summary>
        /// This method aims to update an existing record in database
        /// </summary>
        /// <param name="data">
        /// The EmployeeType object to be updated
        /// </param>
        /// <returns>
        /// A Feedback object containing information about updating result
        /// </returns>
        public Feedback Update(EmployeeType data)
        {
            return null;
        }

        /// <summary>
        /// This method aims to delete an existing record in database
        /// </summary>
        /// <param name="data">
        /// The EmployeeType object to be deleted
        /// </param>
        /// <returns>
        /// A Feedback object containing information about deleting result
        /// </returns>
        public Feedback Delete(EmployeeType data)
        {
            return null;
        }

        public CollectionFeedback<EmployeeType> Search<TCriteria>(TCriteria criteria)
        {
            throw new NotImplementedException();
        }

        public CollectionFeedback<EmployeeType> Search(EmployeeType criteria)
        {
            throw new NotImplementedException();
        }
    }
}
